package com.zz.hhbl.ui.ai

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseMultiItemAdapter
import com.chad.library.adapter4.BaseQuickAdapter
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.OffsetEdgeTreatment
import com.google.android.material.shape.ShapeAppearanceModel
import com.google.android.material.shape.TriangleEdgeTreatment
import com.zz.framework.core.ext.dp
import com.zz.framework.core.ext.onClick
import com.zz.hhbl.config.GlobalCache
import com.zz.hhbl.databinding.ItemAiLeftBinding
import com.zz.hhbl.databinding.ItemAiRightBinding
import com.zz.hhbl.entity.AiSubtitleEntity

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/3/1 20:10.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
class MessageAdapter : BaseMultiItemAdapter<AiSubtitleEntity>() {
    val datas = mutableListOf<AiSubtitleEntity>()
    private val ITEM_TYPE_LEFT = 9998
    private val ITEM_TYPE_RIGHT = 9999

    private class ItemAiLeftViewHolder(val viewBinding: ItemAiLeftBinding) : RecyclerView.ViewHolder(viewBinding.root)
    private class ItemAiRightViewHolder(val viewBinding: ItemAiRightBinding) : RecyclerView.ViewHolder(viewBinding.root)

    init {
        addItemType(ITEM_TYPE_LEFT, object : OnMultiItemAdapterListener<AiSubtitleEntity, ItemAiLeftViewHolder> {
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemAiLeftViewHolder {
                val viewBinding = ItemAiLeftBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemAiLeftViewHolder(viewBinding)
            }

            override fun onBind(holder: ItemAiLeftViewHolder, position: Int, item: AiSubtitleEntity?) {
                val shapeModel = ShapeAppearanceModel.builder()
//                    .setAllCornerSizes(6F.dp())
                    .setBottomLeftCornerSize(9f.dp())
                    .setTopRightCornerSize(9f.dp())
                    .setBottomRightCornerSize(9f.dp())
//                    .setLeftEdge(OffsetEdgeTreatment(TriangleEdgeTreatment(6F.dp(), false), 0f.dp()))
                    .build()
                val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
                    setTint(Color.parseColor("#E6E6E6"))
                    paintStyle = Paint.Style.FILL
                }
                holder.viewBinding.tvContent.apply {
                    onClick {
                        item?.let {
                            mListener?.invoke(item)
                        }
                    }
                    text = item?.text.orEmpty()
                    (parent as ViewGroup).clipChildren = false
                    background = backgroundDrawable
                }
            }
        }).addItemType(ITEM_TYPE_RIGHT, object : OnMultiItemAdapterListener<AiSubtitleEntity, ItemAiRightViewHolder> {
            override fun onBind(holder: ItemAiRightViewHolder, position: Int, item: AiSubtitleEntity?) {
                val shapeModel = ShapeAppearanceModel.builder()
                    .setBottomLeftCornerSize(9f.dp())
                    .setTopLeftCornerSize(9f.dp())
//                    .setTopRightCornerSize(9f.dp())
                    .setBottomRightCornerSize(9f.dp())
//                    .setAllCornerSizes(6F.dp())
//                    .setRightEdge(OffsetEdgeTreatment(TriangleEdgeTreatment(6F.dp(), false), 0f.dp()))
                    .build()
                val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
                    setTint(Color.parseColor("#39429B"))
                    paintStyle = Paint.Style.FILL
                }
                holder.viewBinding.tvContent.apply {
                    text = item?.text.orEmpty()
                    (parent as ViewGroup).clipChildren = false
                    background = backgroundDrawable
                }
            }

            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemAiRightViewHolder {
                val viewBinding = ItemAiRightBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemAiRightViewHolder(viewBinding)
            }
        }).onItemViewType { position, list ->
            if (list[position].userId == GlobalCache.getAiConfig().userId) {
                ITEM_TYPE_RIGHT
            } else {
                ITEM_TYPE_LEFT
            }
        }
    }

    var mListener: ((item: AiSubtitleEntity) -> Unit)? = null
    fun setItemClickListener(listener: (item: AiSubtitleEntity) -> Unit) :MessageAdapter{
        mListener = listener
        return this
    }

    fun getData() = datas

    override fun submitList(list: List<AiSubtitleEntity>?) {
        if (list.isNullOrEmpty()) {
            datas.clear()
        }
        super.submitList(datas)
    }

    fun addData(item: AiSubtitleEntity) {
        if (datas.isNullOrEmpty().not()) {
            if (datas.last().definite) {
                datas.add(item)
            } else {
                datas[items.lastIndex] = item
            }
        } else {
            datas.add(item)
        }
        submitList(datas)
    }

}